package com.huadao.cart_ccs.experiment.sterilization;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseActivity;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.common.dialog.IDialogResultListener;
import com.huadao.cart_ccs.experiment.confirmBeforeCulture.ConfirmBeforeCultureActivity;

public class UltravioletSterilizationActivity extends BaseActivity implements IUltravioletSterilizationContract.IUltravioletSterilizationView {

    private TextView desc;
    private TextView tv_countdown;
    private ImageView UltravioletLight;
    private ImageView UltravioletLight_wick;
    private ImageView wick_scan;

    private int sterilizationTime;//s
    private UltravioletSterilizationPresenter presenter;
//    private Button btn_start;
    private boolean afterCulture;
    private int leftChangeAirSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sterilization);
        desc = findViewById(R.id.desc);
        tv_countdown = findViewById(R.id.tv_countdown);
        UltravioletLight = findViewById(R.id.UltravioletLight);
        UltravioletLight_wick = findViewById(R.id.UltravioletLight_wick);
        wick_scan = findViewById(R.id.wick_scan);
        startScanAnim();

        presenter=new UltravioletSterilizationPresenter(this);

        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
            if(bundle.containsKey("sterilizationTime")) {
                sterilizationTime = bundle.getInt("sterilizationTime");
            }
            if(bundle.containsKey("leftChangeAirSecond")){
                leftChangeAirSecond=bundle.getInt("leftChangeAirSecond");
            }
            if(bundle.containsKey("afterCulture")){
                afterCulture=bundle.getBoolean("afterCulture");
                findViewById(R.id.btn_skip).setVisibility(View.GONE);
            }else {
                afterCulture=false;
            }
        }
        if(leftChangeAirSecond>0){
            presenter.closeChangeAirPumpDelay(leftChangeAirSecond);
        }
        if(sterilizationTime>0){
//            desc.setText("请打开紫外线杀菌");
            setCountDown(sterilizationTime);
        }else {
            desc.setText("请设置紫外线照射时间");
        }

        findViewById(R.id.btn_skip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.closeLight();
                onUltravioletOff();
            }
        });
//        btn_start=findViewById(R.id.btn_start);
//        btn_start.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                promoteTurnOnLight();
//            }
//        });
        presenter.runUltraviolet();
//        onUltravioletOn();
    }

//    private void promoteTurnOnLight(){
//        DialogFragmentHelper.showConfirmDialog(this, "开始杀菌", "确认开启紫外线灯开始杀菌吗？", new IDialogResultListener<Integer>() {
//            @Override
//            public void onDataResult(Integer result) {
//                if(result==DialogInterface.BUTTON_POSITIVE){
//                    presenter.runUltraviolet();
//                    btn_start.setEnabled(false);
//                }
//            }
//        },false);
//    }

    @Override
    public int getUltravioletTime() {
        return sterilizationTime;
    }

    @Override
    public void onUltravioletOn() {
        UltravioletLight_wick.setVisibility(View.VISIBLE);
        desc.setText("紫外线照射中...");
        showToast("开始杀菌");
        startScanAnim();
    }

    private void startScanAnim(){
        Animation translateAnimation = AnimationUtils.loadAnimation(this,R.anim.light_scan);
        wick_scan.setAnimation(translateAnimation);
        wick_scan.startAnimation(translateAnimation);
    }

    private void stopScanAnim(){
        wick_scan.clearAnimation();
    }

    @Override
    public void onUltravioletOff() {
        stopScanAnim();
        UltravioletLight_wick.setVisibility(View.INVISIBLE);
        desc.setText("紫外线关闭");
        showToast("杀菌结束");
        if(!afterCulture) {
            jumpToConfirm();
        }else{
            DialogFragmentHelper.showMessageDialog(this,"","马上关机");
            presenter.shunDown();
        }
    }

    @Override
    public void onCloseChangeAirPump() {
        showToast("换气结束");
    }

    @Override
    public void sterilizationCountDown(int countDown) {
        setCountDown(countDown);
    }

    private void setCountDown(int countDown){
        int minutes=countDown/60;
        int seconds=countDown-minutes*60;
        tv_countdown.setText(String.format("%02d:%02d",minutes,seconds));
    }

    private void jumpToConfirm(){
        Intent intent=new Intent(this,ConfirmBeforeCultureActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presenter!=null){
            presenter.detachView();
        }
    }
}
